﻿/*=========================
auth: johnny
Desc: 用户注册
=========================*/
var aCity = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北",
    43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外"
}

var regFlag = false;
$(document).ready(function () {

    InitializeReg();
    CheckInput();
})


//初始化
function InitializeReg() {
    $("#MainContent_MainContent_txt_userName").val("");
    $("#MainContent_txt_pwd").val("");
    CaptChaImage();
}


//获取验证码
function CaptChaImage() {
    JSONWebServiceA("../Handler/Captcha.ashx", "{}", function (objJSON) {
        $('span[id=lblCaptCha]').html(objJSON);
    });
}




function OnConfirmClick() {
    if (!$('#ckbAgreement').attr("checked")) {
        alert('您必须同意注册协议才能注册！');
        return false;
    }
    else {
        ValidateInputData();
        if (regFlag == true || regFlag == 1) {
            return true;
        }
        else {
            alert("请检查注册资料是否填写正确");
            return false;
        }
    }
}


function ValidateInputData() {
    regFlag = true;
    OnuserNameValidate();
    OnPasswordValidate();
    OnRePasswordValidate();
    OnCodeValidate();
    OnssnValidate();
}

//检查各项输入项是否正确
function CheckInput() {
    //用户名
    $("#MainContent_txt_userName").blur(function () {
        OnuserNameValidate();
    })

    //密码
    $("#MainContent_txt_Password").blur(function () {
        OnPasswordValidate();
    })

    //确认密码
    $("#MainContent_txt_RePassword").blur(function () {
        OnRePasswordValidate();
    })

    //身份证
    $("#MainContent_txt_ssn").blur(function () {
        OnssnValidate();
    })

    //验证码
    $("#MainContent_txtValidate").blur(function () {
        OnCodeValidate();
    })
}



function OnuserNameValidate() {
    var patterns = new RegExp("^[a-zA-Z0-9]{4,15}$");
    if (patterns.test($("#MainContent_txt_userName").val()) == false) {
        $("#accountEImg").css("display", "inline"); ;
        $("#accountEImg").attr("src", "../App_Themes/images/false.gif");
        $("#spanAccount").html("用户名英文或数字,长度为4~15的字符！");
        $("#spanAccount").css("color", "Red");
        regFlag = regFlag & false;
    }
    else {
//        JSONWebService("../Handler/CheckTheAccountIsCanUse.ashx", "{userName:'" + $('#MainContent_txt_userName').val() + "'}", function (objJSON) {
        JSONWebServiceA("../Handler/CheckTheAccountIsCanUse.ashx", { userName: $("#MainContent_txt_userName").val() }, function (objJSON) {
         
            if (objJSON == "0") {
                $("#accountEImg").css("display", "inline");
                $("#accountEImg").attr("src", "../App_Themes/images/true.gif");
                $("#spanAccount").html("可以使用！");
                $("#spanAccount").css("color", "Green");
                regFlag = regFlag & true;
            }
            else {
                $("#accountEImg").css("display", "inline"); ;
                $("#accountEImg").attr("src", "../App_Themes/images/false.gif");
                $("#spanAccount").html("此用户名已被使用,请重新输入！");
                $("#spanAccount").css("color", "Red");
                regFlag = regFlag & false;
            }
        });
    }
}


//检查密码
function OnPasswordValidate() {
    var patterns = new RegExp("^[a-zA-Z0-9]{6,16}$");
    if (patterns.test($("#MainContent_txt_Password").val()) == false) {
        $("#pwdEImg").css("display", "inline");
        $("#pwdEImg").attr("src", "../App_Themes/images/false.gif");
        $("#spanPwd").html("密码不符合规范，密码必须为6~16位英文+数字的组合！");
        $("#spanPwd").css("color", "Red");
        regFlag = regFlag & false;
    }
    else {
        $("#pwdEImg").css("display", "inline");
        $("#pwdEImg").attr("src", "../App_Themes/images/true.gif");
        $("#spanPwd").html("填写正确！");
        $("#spanPwd").css("color", "Green");
        regFlag = regFlag & true;
    }
}


//密码确认
function OnRePasswordValidate() {
    if ($.trim($("#MainContent_txt_RePassword").val()).length > 0) {
        if ($("#txtPassword").val() != $("#txtRePassword").val()) {
            $("#rePwdEImg").css("display", "inline");
            $("#rePwdEImg").attr("src", "../App_Themes/images/false.gif");
            $("#spanRePwd").html("密码和确认密码不一致！");
            $("#spanRePwd").css("color", "Red");
            regFlag = regFlag & false;
        }
        else {
            $("#rePwdEImg").css("display", "inline");
            $("#rePwdEImg").attr("src", "../App_Themes/images/true.gif");
            $("#spanRePwd").html("填写正确！");
            $("#spanRePwd").css("color", "Green");
            regFlag = regFlag & true;
        }
    }
    else {
        $("#rePwdEImg").css("display", "inline");
        $("#rePwdEImg").attr("src", "../App_Themes/images/false.gif");
        $("#spanRePwd").html("请填写确认密码！");
        $("#spanRePwd").css("color", "Red");
        regFlag = regFlag & false;
    }
}

//验证码
function OnCodeValidate() {
    if ($("#MainContent_txtValidate").val().length == 0) {
        $("#valEImg").css("display", "inline");
        $("#valEImg").attr("src", "../App_Themes/images/false.gif");
        $("#validateMsg").html("请填写验证码！");
        $("#validateMsg").css("color", "Red");
    }
    if ($("#MainContent_txtValidate").val().length != 4) {
        $("#valEImg").css("display", "inline");
        $("#valEImg").attr("src", "../App_Themes/images/false.gif");
        $("#validateMsg").html("请填写正确验证码！");
        $("#validateMsg").css("color", "Red");
        regFlag = regFlag & false;
    }
    else {
        $("#valEImg").css("display", "none");
        $("#validateMsg").html("");
        regFlag = regFlag & true;
    }
}



//验证身份证号码
function OnssnValidate() {
    //    var patterns = new RegExp("^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$");
//    var patterns = new RegExp("^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$");


//    if (patterns.test($("#MainContent_txt_ssn").val()) == false) {
//        $("#ssnImage").css("display", "inline");
//        $("#ssnImage").attr("src", "../App_Themes/images/false.gif");
//        $("#spanSSN").html("请填写正确的身份证号码！");
//        $("#spanSSN").css("color", "Red");
//        regFlag = regFlag & false;
//    }
//    else {
//        $("#ssnImage").css("display", "inline");
//        $("#ssnImage").attr("src", "../App_Themes/images/true.gif");
//        $("#spanSSN").html("填写正确！");
//        $("#spanSSN").css("color", "Green");
//        regFlag = regFlag & true;
    //    }

    var ssnflag = cidInfo($("#MainContent_txt_ssn").val());
    if (ssnflag == true) {
        $("#ssnImage").css("display", "inline");
        $("#ssnImage").attr("src", "../App_Themes/images/true.gif");
        $("#spanSSN").html("填写正确！");
        $("#spanSSN").css("color", "Green");
        regFlag = regFlag & true;
    }
    else {
        $("#ssnImage").css("display", "inline");
        $("#ssnImage").attr("src", "../App_Themes/images/false.gif");
        $("#spanSSN").html("请填写有效的18位身份证信息！");
        $("#spanSSN").css("color", "Red");
        regFlag = regFlag & false;
    }

}




function cidInfo(sId) {
    if (sId.length != 18) {
        return "Error:非法证号";
    }

    var iSum = 0

    var info = ""

    sId = sId.replace(/x$/i, "a");

    if (aCity[parseInt(sId.substr(0, 2))] == null) return "Error:非法地区";

    sBirthday = sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2));

    var d = new Date(sBirthday.replace(/-/g, "/"))

    if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) return "Error:非法生日";

    for (var i = 17; i >= 0; i--) iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11)

    if (iSum % 11 != 1) return "Error:非法证号";

    return true;
}
